
//Changed to Relative layout
package com.widget;


import java.io.*;



import android.app.Activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;


public class MainActivity extends Activity implements OnClickListener{
    
	/** Called when the activity is first created. */
	
	Bundle bundle;
	String path;
	String IP_ADDR;
	String USERNAME;
	ManageProcess process;
	
	private MSGHandler mMsgHandler;
	    
	@Override
    public void onCreate(Bundle savedInstanceState) {
		
        super.onCreate(savedInstanceState);      
        
        bundle = new Bundle();
        process = new ManageProcess();
        process.acquireRoot();
        setContentView(R.layout.main);
         
        bundle=this.getIntent().getExtras();
        IP_ADDR=bundle.getString("IPADDR");
        USERNAME=bundle.getString("USERNAME");
        
        
        
		try {
			FileInputStream fins = new FileInputStream(this.getApplicationContext().getFilesDir()+"/bin/rsync");
			int size;
			size = fins.available();
			fins.close();
			path = this.getApplicationContext().getFilesDir() + "/bin/rsync";
		} catch (IOException e) {
			
			try {
				path = this.getApplicationContext().getFilesDir() + "/bin/";
				File file = new File(path);
				file.mkdirs();
				path += "rsync";
				//Merge the 2 parts together
				/* A little patched up code cleanup required */
				
				InputStream ins2 = getResources().openRawResource(R.raw.rsyncpart2);
				int size2;
				size2 = ins2.available();
				byte[] buffer2 = new byte[size2];
				ins2.read(buffer2);
				ins2.close();
				
				InputStream ins1 = getResources().openRawResource(R.raw.rsyncpart1);
				int size1;
				size1 = ins1.available();
				byte[] buffer1 = new byte[size1];
				ins1.read(buffer1);
				ins1.close();
				
				
				byte[] buffer = new byte[size1+size2];
				for(int i=0;i<size1;i++)
				{
					buffer[i] = buffer1[i];
				}
				for(int i=size1,j=0;j<size2;i++,j++)
				{
					buffer[i] = buffer2[j];
				}
				
				
				FileOutputStream fos = new FileOutputStream(path);
				fos.write(buffer);
				fos.close();
				process.executeCmd("chmod 777 "+path);
				//String outputCmd="";
				//String bytesRead = process.readCmd(outputCmd);
			} catch (IOException e1) {
			
				e1.printStackTrace();
			}
			
		}
		bundle.putString("PATH", path);
        bundle.putString("IPADDR",IP_ADDR);
        bundle.putString("USERNAME", USERNAME);
        
        mMsgHandler = new MSGHandler(this);
        //alertDialog = new AlertDialog.Builder(this);
        
    }

	@Override
	public void onClick(View v) {
		
		String output="";
		
		switch(v.getId()){
			
			case R.id.apkSyncAll:
				final ProgressDialog dialog1 = ProgressDialog.show(this, "Syncing...", 
						"Please wail while all applications are synchronized. This dialog will " +
						" automatically disappear once the sync is done.", true);
				
				
				mMsgHandler.setDialog(dialog1);
				ParseStreaming p = new ApkParser();
				p.endMatcher(3, mMsgHandler);
				process.setParser(p);
				process.executeCmd(path+" -aP --delete --delete-excluded /data/app/*.apk rsync://"+USERNAME+"@"+IP_ADDR+"/"+USERNAME+"/backup/data/app/");
				Log.d("Sync",output);
				process.executeCmd(path+" -aP --delete --delete-excluded /system/app/*.apk rsync://"+USERNAME+"@"+IP_ADDR+"/"+USERNAME+"/backup/system/app/");
				Log.d("Sync",output);
				process.executeCmd(path+" -aP --delete --delete-excluded /mnt/asec/ rsync://"+USERNAME+"@"+IP_ADDR+"/"+USERNAME+"/backup/mnt/asec/");
				Log.d("Sync",output);
				
				
			break;
			
			case R.id.dataSync:
				
			try {
				Class testClass = Class.forName("com.widget.ListAppsActivity");
				Intent testIntent = new Intent(MainActivity.this,testClass);
				testIntent.putExtras(bundle);
				startActivity(testIntent);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			break;
			
			case R.id.appsOnCloud:
				try {
					Class testClass = Class.forName("com.widget.NewAppsOnCloud");
					Intent testIntent = new Intent(MainActivity.this,testClass);
					testIntent.putExtras(bundle);
					startActivity(testIntent);
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			break;
			
			case R.id.syncAllData:
				
				final ProgressDialog dialog2 = ProgressDialog.show(this, "Syncing...", 
						"Please wail while all applications are synchronized. This dialog will " +
						" automatically disappear once the sync is done.", true);
				mMsgHandler.setDialog(dialog2);
				p = new DataParser();
				p.endMatcher(1, mMsgHandler);
				process.setParser(p);
				process.executeCmd(path+" -aP --delete --delete-excluded  /data/data/ rsync://"+USERNAME+"@"+IP_ADDR+"/"+USERNAME+"/backup/data/data/");
						
			break;
			
			
			
			case R.id.apkSync:
				try{
					Class apkSyncClass = Class.forName("com.widget.apkSyncActivity");
					Intent apkSyncIntent = new Intent(MainActivity.this, apkSyncClass);
					apkSyncIntent.putExtras(bundle);
					startActivity(apkSyncIntent);
				}catch(ClassNotFoundException e){
					e.printStackTrace();
				}
			break;
		}
	}
	
}